<?php
class Zendvn_Menu_Public{
	
	private $_menuDb;
	
	private $_options;

	private $_helperUrl;
	
	
	
	public function __construct($menuDb = null, $options = null){
		
		$this->_menuDb = $menuDb;
		$this->_options = $options;
		$this->_helperUrl = new Zend_View_Helper_Url();
		$this->_helperTranslate = new Zend_View_Helper_Translate();
		
	}
	
	public function create($parents = 0){
		$newMenu = null;
		$this->recursiveMenu($this->_menuDb,$parents,1,$newMenu);		
		$newMenu = str_replace('<ul></ul>','',$newMenu);
		return $newMenu;
	}
	
	protected function recursiveMenu($sourceArr,$parents = 0,$step = 1, &$newMenu){
		
		
		if(count($sourceArr)>0){
			if($step == 1){
				$newMenu .= '<ul>';
			}else{
				$newMenu .= '<ul>';
			}
			foreach ($sourceArr as $key => $value){
				$link = '#';
				if($value['type'] == 'module'){
					$arrayParam = array(
										'module'=> $value['module'],
										'controller'=>$value['controller'],
										'action'=>$value['action'],
										);
										
					$routeName = ($value['router']=='none')?null:$value['router'];									
					
					$link = $this->_helperUrl->url($arrayParam,$routeName,true);
				}
				
				if($value['type'] == 'link'){
					$link = $value['url'];
				}
				
				$step = $step + 1;
				if($value['parents'] == $parents){
					$newMenu .= '<li><a href="' . $link .'">' 
								. $this->_helperTranslate->translate($value['name'])
								. '</a>';
					$newParents = $value['id'];
					unset($sourceArr[$key]);
					$this->recursiveMenu($sourceArr,$newParents, $step, $newMenu);
					$newMenu .= '</li>';
				}
			}
			$newMenu .= '</ul>';
		}
	}
}